package com.xj.api.bussiness.deviceInformation.web;

import com.xj.api.bussiness.deviceInformation.service.ITbDeviceInformationService;
import com.xj.common.bussiness.deviceInformation.entity.DeviceInfoMould;
import com.xj.common.bussiness.deviceInformation.entity.DeviceInfoParamMould;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.xj.api.base.web.BaseController;
import com.xj.common.base.common.bean.AbstractBean;
import com.xj.common.bussiness.deviceInformation.entity.TbDeviceInformation;

import io.swagger.annotations.ApiOperation;

import java.util.List;

/**
 * <p>
 *   前端控制器
 * </p>
 *
 * @author Hmemb
 * @since 2018-03-16
 */
@RestController
@RequestMapping("deviceInformation")
public class TbDeviceInformationController extends BaseController {

	@Autowired
	private ITbDeviceInformationService deviceInformationService;

	@GetMapping
	@ApiOperation(value = "  列表", httpMethod = "GET", notes = "  列表")
	public AbstractBean deviceInformationList(@RequestParam(required = false, defaultValue = "0") Integer pageNum, @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
		return json(deviceInformationService.selectMapsPage(new Page<TbDeviceInformation>(pageNum, pageSize), new EntityWrapper<TbDeviceInformation>()));
	}

	@GetMapping("/{id}.auth")
	@ApiOperation(value = "  詳情", httpMethod = "GET", notes = "  詳情")
	public AbstractBean deviceInformationContent(@PathVariable Integer id) {
		return json(deviceInformationService.selectById(id));
	}

	@PostMapping("save")
	@ApiOperation(value = "  新增", httpMethod = "POST", notes = " 新增")
	public AbstractBean add(TbDeviceInformation tbdeviceinformation) {
		if(deviceInformationService.getDeviceInformationByCondition(tbdeviceinformation).size()>0){
			return error();
		}else{
			if (!deviceInformationService.insertOrUpdate(tbdeviceinformation)) {
				return error();
			}
			return success();
		}
	}

	@DeleteMapping("{id}/delete")
	@ApiOperation(value = "  删除", httpMethod = "POST", notes = " 删除")
	public AbstractBean delete(@PathVariable(required = true) Integer id) {
		if (!deviceInformationService.deleteById(id)) {
			return error();
		}
		return success();
	}

	@PostMapping("{identification}/getDeviceInformationByCondition")
	@ApiOperation(value = " 查询", httpMethod = "POST", notes = " 查询")
	public AbstractBean getDeviceInformationByCondition(@PathVariable(required = true) String identification) {
		if (!identification.equals("") && identification != null) {
			TbDeviceInformation deviceInformation = new TbDeviceInformation();
			deviceInformation.setIdentification(identification);
			List<TbDeviceInformation> list = deviceInformationService.getDeviceInformationByCondition(deviceInformation);
			if (list != null && list.size() > 0) {
				return json(list);
			} else {
				return error();
			}
		} else {
			return error();
		}
	}

	@PostMapping("{factoryNum}/getStatusByCondition")
	@ApiOperation(value = " 出厂编号查询返回状态", httpMethod = "POST", notes = " 出厂编号查询返回状态")
	public AbstractBean getStatusByCondition(@PathVariable(required = true) String factoryNum) {
		if (!factoryNum.equals("") && factoryNum != null) {
			TbDeviceInformation deviceInformation = new TbDeviceInformation();
			deviceInformation.setFactoryNum(factoryNum);
			List<TbDeviceInformation> list = deviceInformationService.getDeviceInformationByCondition(deviceInformation);
			if (list != null && list.size() > 0) {
				return success();
			} else {
				return fail("该号码不存在", "1");
			}
		} else {
			return error();
		}
	}

	@PostMapping("{plateNum}/getCarStatusByCondition")
	@ApiOperation(value = " 设备牌编号查询返回状态", httpMethod = "POST", notes = " 设备牌编号查询返回状态")
	public AbstractBean getCarStatusByCondition(@PathVariable(required = true) String plateNum) {
		if (!plateNum.equals("") && plateNum != null) {
			TbDeviceInformation deviceInformation = new TbDeviceInformation();
			deviceInformation.setPlateNum(plateNum);
			List<TbDeviceInformation> list = deviceInformationService.getDeviceInformationByCondition(deviceInformation);
			if (list != null && list.size() > 0) {
				return success();
			} else {
				return fail("该号码不存在", "1");
			}
		} else {
			return error();
		}
	}

	@PostMapping("{plateNum}/getCarJsonStatusByCondition")
	@ApiOperation(value = " 设备牌编号查询返回Json", httpMethod = "POST", notes = " 设备牌编号查询返回Json")
	public AbstractBean getCarJsonStatusByCondition(@PathVariable(required = true) String plateNum) {
		if (!plateNum.equals("") && plateNum != null) {
			TbDeviceInformation deviceInformation = new TbDeviceInformation();
			deviceInformation.setPlateNum(plateNum);
			List<TbDeviceInformation> list = deviceInformationService.getDeviceInformationByCondition(deviceInformation);
			if (list != null && list.size() > 0) {
				return json(list);
			} else {
				return fail("该号码不存在", "0");
			}
		} else {
			return error();
		}
	}

	@GetMapping("/deviceInfoPage")
	@ApiOperation(value = "  根据特定字段查询设备详情、图片列表", httpMethod = "GET", notes = "  根据特定字段查询设备详情、图片列表")
	public AbstractBean deviceInfoPage(@RequestParam(required = false, defaultValue = "0") Integer pageNum, @RequestParam(required = false, defaultValue = "10") Integer pageSize,DeviceInfoParamMould deviceInfoParamMould) {
		return json(deviceInformationService.selectDeviceInfo(new Page<DeviceInfoMould>(pageNum, pageSize),deviceInfoParamMould ));
	}
}
